package com.wyw.leetcode.learning.simple;

/**
 * leetcode topic  733
 * 图像渲染，不得不说，题意真没读懂，直接看的评论
 * @Author Mr Wu （yewen.wu.china@gmail.com）
 * @Date 2021/11/17 14:49
 */
public class Topic733 {
    public static void main(String[] args) {
        int image[][] = new int[][] {{1,1,1}, {1,1,0}, {1,0,1}};
        System.out.println(floodFill(image, 1, 1, 2));
    }

    public static int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        dfs(image, sr, sc, newColor, image[sr][sc]);
        return image;
    }

    public static void dfs(int[][] image, int sr, int sc, int newColor, int oldColor) {
        if(sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length)
            return;
        if(image[sr][sc] != oldColor || image[sr][sc] == newColor)
            return;
        image[sr][sc] = newColor;
        dfs(image, sr - 1, sc, newColor, oldColor);
        dfs(image, sr + 1, sc, newColor, oldColor);
        dfs(image, sr, sc - 1, newColor, oldColor);
        dfs(image, sr, sc + 1, newColor, oldColor);
    }
}
